package com.lazy.utils.token.client;

import com.lazy.utils.interceptor.User;
import com.lazy.utils.token.jwt.JWTHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.axis.utils.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * token 工具类
 *
 * @author 张凯
 * @Date: Created in 16:08 2018/5/22
 */
@Component("clientTokenUtil")
@Slf4j
public class ClientTokenUtil {

	@Value("${token.client.expire}")
	private int expire;
	@Value("${token.client.pri-key.path}")
	private String priKeyPath;
	@Value("${token.client.pub-key.path}")
	private String pubKeyPath;
	@Value("${token.client.secret}")
	private String pass;

	/**
	 * 生成token
	 *
	 * @param id
	 * @param phone
	 * @return
	 * @throws Exception
	 */
	public String generateToken(Integer id, String phone) throws Exception {
		return JWTHelper.generateToken(id, phone, priKeyPath, expire);
	}

	/**
	 * 解析token
	 * @param token
	 * @return
	 * @throws Exception
	 */
	public User getInfoFromToken(String token) throws Exception {
        log.info("解析token:{}",token);
        if(StringUtils.isEmpty(token)){
            return null;
        }
        return JWTHelper.getInfoFromToken(token,pubKeyPath);
    }

	/**
	 * 生成公钥和私钥
	 */
	public void generateKey() {
		log.info("生成公钥和私钥");
		JWTHelper.generateKey(pubKeyPath, priKeyPath, pass);
	}

}
